package github.leavesczy.reactivehttpsamples.util

import android.graphics.Color
import androidx.core.view.get
import com.blankj.utilcode.util.ColorUtils
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.ValueFormatter
import github.leavesczy.reactivehttpsamples.R
import github.leavesczy.reactivehttpsamples.core.mode.SensorLocation


public class SensorLocationLineChart(private val lineChart: LineChart){

    private val xMaxDefault=10f
    private val yMinDefault=-0.5f
    private val yMaxDefault=0.5f
    private var maxPont:Int=100;

    init {
        initChart()
    }

    private val sets= mutableMapOf<String,SensorLocationDataSet>()

    private fun initChart() {

        /**
         * 图表设置
         */
        lineChart.description.isEnabled=false
        //图表边框
        lineChart.setDrawBorders(false)
        //图表距离上下左右的距离
        lineChart.setExtraOffsets(5f,0f,5f,0f)
        lineChart.setTouchEnabled(true)
        lineChart.setScaleEnabled(false)
        lineChart.isScaleXEnabled=true
        lineChart.isDragEnabled=false
        lineChart.setNoDataText("暂无数据")
        lineChart.setNoDataTextColor(ColorUtils.getColor(R.color.c5C9BEE))
        lineChart.isLogEnabled=true

        /**
         * 图例设置
         */
        val legend = lineChart.legend
        legend.form= Legend.LegendForm.LINE
        legend.formSize=10f
        legend.formLineWidth=5f
        legend.textSize=12f
        legend.verticalAlignment= Legend.LegendVerticalAlignment.BOTTOM
        legend.horizontalAlignment= Legend.LegendHorizontalAlignment.CENTER
        legend.orientation= Legend.LegendOrientation.HORIZONTAL
        legend.setDrawInside(false)
        legend.isWordWrapEnabled=true


        val xAxis = lineChart.xAxis
        xAxis.position= XAxis.XAxisPosition.BOTTOM
        //xAxis.valueFormatter=XAxisVa
//        xAxis.axisMinimum=0f
        xAxis.axisMaximum=xMaxDefault
        xAxis.setDrawGridLines(true)
        xAxis.granularity=1f
        xAxis.gridColor= ColorUtils.getColor(R.color.bittersweet)
        xAxis.setDrawLabels(true)
        xAxis.valueFormatter=XAxisValueFormatter()


        val yLeftAxis = lineChart.axisLeft

        yLeftAxis.axisMinimum=yMinDefault
        yLeftAxis.axisMaximum=yMaxDefault
        yLeftAxis.setDrawGridLines(true)
        yLeftAxis.granularity=1f
        yLeftAxis.gridColor= ColorUtils.getColor(R.color.c108EE9)
        yLeftAxis.setDrawLabels(true)
        yLeftAxis.valueFormatter=YAxisValueFormatter()



        lineChart.axisRight.isEnabled=false
    }

    fun initNoData(){
        val xAxis = lineChart.xAxis
        xAxis.axisMaximum=xMaxDefault

        val yLeftAxis = lineChart.axisLeft
        yLeftAxis.axisMinimum=yMinDefault
        yLeftAxis.axisMaximum=yMaxDefault

        initLineData()

    }

    fun initLineData(){
        val xSet= SensorLocationDataSet("x方向",ColorUtils.getColor(R.color.c5C9BEE),maxPont)
        val ySet= SensorLocationDataSet("y方向",ColorUtils.getColor(R.color.c975F5D),maxPont)
        val zSet= SensorLocationDataSet("z方向",ColorUtils.getColor(R.color.cFBAB00),maxPont)
        xSet.addData(0,0f)
        ySet.addData(0,0f)
        zSet.addData(0,0f)
        sets.clear()
        sets.put("x方向",xSet)
        sets.put("y方向",ySet)
        sets.put("z方向",zSet)
        val lineData= LineData(xSet.getLineDataSet(),ySet.getLineDataSet(),zSet.getLineDataSet())

        lineChart.clear()
        lineChart.data=lineData
        updateChart()

    }

    fun addData(data: SensorLocation){
        var yMax=lineChart.axisLeft.mAxisMaximum
        var yMin=lineChart.axisLeft.axisMinimum

        val diffMilli = data.diffTimeMilli
        sets.keys.forEach {
            val sensorLocationDataSet = sets.get(it)
            when(it){
                "x方向"->{
                    sensorLocationDataSet!!.addData(diffMilli,data.xAxis)
                    if(yMax<data.xAxis){
                        yMax=data.xAxis+0.5f
                    }else if(yMin>data.xAxis){
                        yMin=data.xAxis-0.5f
                    }
                }
                "y方向"->{
                    sensorLocationDataSet!!.addData(diffMilli,data.yAxis)
                    if(yMax<data.yAxis){
                        yMax=data.yAxis+0.5f
                    }else if(yMin>data.yAxis){
                        yMin=data.yAxis-0.5f
                    }
                }
                "z方向"->{
                    sensorLocationDataSet!!.addData(diffMilli,data.zAxis)
                    if(yMax<data.zAxis){
                        yMax=data.zAxis+0.5f
                    }else if(yMin>data.zAxis){
                        yMin=data.zAxis-0.5f
                    }
                }
            }

        }
        //扩大 x轴的刻度
        val diffSecond = diffMilli / 1000
        if(lineChart.xAxis.axisMaximum<diffSecond){
            lineChart.xAxis.axisMaximum=diffSecond.toFloat()
        }
        if(lineChart.axisLeft.axisMaximum<yMax){
            lineChart.axisLeft.axisMaximum=yMax
        }
        if(lineChart.axisLeft.axisMinimum>yMin){
            lineChart.axisLeft.axisMinimum=yMin
        }
        updateChart()
    }


    fun addDataBatch(datas: MutableList<SensorLocation>){
        var yMax=lineChart.axisLeft.mAxisMaximum
        var yMin=lineChart.axisLeft.axisMinimum
        var diffMilliMax:Long=0

        //把所有的数据添加到图表集合中
        datas.forEach { data->

            diffMilliMax = data.diffTimeMilli
            sets.keys.forEach {
                val sensorLocationDataSet = sets.get(it)
                when(it){
                    "x方向"->{
                        sensorLocationDataSet!!.addData(diffMilliMax,data.xAxis)
                        if(yMax<data.xAxis){
                            yMax=data.xAxis+0.5f
                        }else if(yMin>data.xAxis){
                            yMin=data.xAxis-0.5f
                        }
                    }
                    "y方向"->{
                        sensorLocationDataSet!!.addData(diffMilliMax,data.yAxis)
                        if(yMax<data.yAxis){
                            yMax=data.yAxis+0.5f
                        }else if(yMin>data.yAxis){
                            yMin=data.yAxis-0.5f
                        }
                    }
                    "z方向"->{
                        sensorLocationDataSet!!.addData(diffMilliMax,data.zAxis)
                        if(yMax<data.zAxis){
                            yMax=data.zAxis+0.5f
                        }else if(yMin>data.zAxis){
                            yMin=data.zAxis-0.5f
                        }
                    }
                }

            }
        }

        //扩大 x轴的刻度
        val diffSecond = diffMilliMax / 1000
        if(lineChart.xAxis.axisMaximum<diffSecond){
            lineChart.xAxis.axisMaximum=diffSecond.toFloat()
        }
        if(lineChart.axisLeft.axisMaximum<yMax){
            lineChart.axisLeft.axisMaximum=yMax
        }
        if(lineChart.axisLeft.axisMinimum>yMin){
            lineChart.axisLeft.axisMinimum=yMin
        }
        updateChart()
    }





    fun updateChart(){
        val lineData=lineChart.data
        lineData?.let {
            it.notifyDataChanged()
            lineChart.notifyDataSetChanged()
            lineChart.invalidate()
        }
    }

    fun setMaxPoint(maxPont: Int) {
        this.maxPont=maxPont
    }


}

public class XAxisValueFormatter :ValueFormatter(){
    override fun getAxisLabel(value: Float, axis: AxisBase?): String {
        return "${value.toInt()}秒"
    }
}

public class YAxisValueFormatter :ValueFormatter(){
    override fun getAxisLabel(value: Float, axis: AxisBase?): String {
        return "${value.toInt()}(g)"
    }
}

public class SensorLocationDataSet(label:String,lineColor:Int,val maxPont: Int){
    private lateinit var lineDataSet: LineDataSet
    private val dataList= mutableListOf<SensorLocation>()
    init {
        lineDataSet=createLineDataSet(label,lineColor)
    }

    private fun createLineDataSet(label:String,lineColor:Int): LineDataSet {
        val set=LineDataSet(mutableListOf(), label)
        set.axisDependency=YAxis.AxisDependency.LEFT
        set.color=lineColor
        //set.setCircleColor(lineColor)
        set.lineWidth=1.5f
       // set.circleRadius=2f
        set.valueTextSize=10f
        set.setDrawCircles(false)
        set.mode=LineDataSet.Mode.HORIZONTAL_BEZIER



        set.isHighlightEnabled=true
        set.highLightColor=Color.RED
        set.highlightLineWidth=1f
        set.setDrawFilled(false)

        //不在数据点 上显示数值
        set.setDrawValues(false)

        return set
    }

    fun getLineDataSet():LineDataSet{
        return lineDataSet
    }
    fun addData(diffMilli:Long,yValue:Float){
        val entry=Entry((diffMilli*1.0f)/1000,yValue)
        lineDataSet.addEntry(entry)

        if(lineDataSet.entryCount>maxPont){
            lineDataSet.removeFirst()
        }
    }

}

